package cn.sstir.hackathon.controller;

import cn.sgst.tool.common.response.Response;
import cn.sstir.hackathon.core.ExportType;
import cn.sgst.tool.common.util.ToolUtil;
import cn.sstir.hackathon.core.http.ClientService;
import cn.sstir.hackathon.core.page.PageResult;
import cn.sstir.hackathon.core.search.PaperQuery;
import cn.sstir.hackathon.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import java.net.URLEncoder;

/**
 * @author: fli
 * @email: fli@sstir.cn
 * @date: 2020/1/16 16:43
 */
@Controller
@RequestMapping("/paper")
public class PaperController {

    @Autowired
    private PaperService paperService;

    @Autowired
    private ClientService clientService;

    @RequestMapping("/search")
    @ResponseBody
    public Response search(PaperQuery paperQuery) throws Exception{
        PageResult pageResult = paperService.search(paperQuery);
        return Response.success(pageResult);
    }

    @RequestMapping("/paperSearch")
    @ResponseBody
    public String paperSearch(String keyword) throws Exception{
         String url = "";

        if(ToolUtil.isEmpty(keyword)){
             url = "http://10.166.166.215:1234/xueshu";
        }else{
             url = "http://10.166.166.215:1234/xueshu?keyword="+keyword;
        }
        String result =  clientService.doGet(url);

        return  result;
    }

    @RequestMapping("/year")
    @ResponseBody
    public String yearSearch(String keyword) throws Exception{
        String url = "";
        if(ToolUtil.isEmpty(keyword)){
            url = "http://10.166.166.215:1234/year";
        }else{
            url = "http://10.166.166.215:1234/year?keyword="+keyword;
        }
        String result =  clientService.doGet(url);
        return  result;
    }

    @RequestMapping("/keyword")
    @ResponseBody
    public String keyword(String keyword) throws Exception{
        String url = "";
        if(ToolUtil.isEmpty(keyword)){
            url = "http://10.166.166.215:1234/hot";
        }else{
            url = "http://10.166.166.215:1234/hot?keyword="+keyword;
        }
        String result =  clientService.doGet(url);
        return  result;
    }




    /**
     * 导出excel
     *
     * @author fli
     * @date 2020/1/17 0:52
     */
    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse response, @RequestParam List<String> columns, PaperQuery patentQuery) throws Exception{
        paperService.export(response,columns,patentQuery,ExportType.excel);
    }


    /**
     * 导出csv
     *
     * @author fli
     * @date 2020/1/17 0:52
     */
    @GetMapping("/export/csv")
    public void exportCsv(HttpServletResponse response, @RequestParam List<String> columns, PaperQuery paperQuery) throws Exception{
        paperService.export(response,columns,paperQuery,ExportType.csv);
    }

    /**
     * 导出csv
     *
     * @author fli
     * @date 2020/1/17 0:52
     */
    @GetMapping("/export/tsv")
    public void exportTsv(HttpServletResponse response, @RequestParam List<String> columns,PaperQuery paperQuery) throws Exception{
        paperService.export(response,columns,paperQuery,ExportType.tsv);
    }


}
